package cn.lj.controller;

import cn.lj.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @author lj
 * @Description:
 * @date 2020-12-03 12:58
 * @QQ 851477108
 */

@Controller
@RequestMapping(path = "/user")
public class HelloController {
    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("Hello MVC");
        return "success";
    }

    @RequestMapping(value="/testRequestMapping",params = {"username=lj"},headers = {"Accept"})
    public String testUser(){
        System.out.println("RequestMapping");
        return "success";
    }

    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(name="username") String username){
        System.out.println("RequestParam");
        System.out.println(username);
        return "success";
    }

    @RequestMapping(value="/testPathVariable/{id}")
    public String testPathVariable(@PathVariable(name="id") String id){
        System.out.println("PathVariable");
        System.out.println(id);
        return "success";
    }

    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String body){
        System.out.println("RequestBody");
        System.out.println(body);
        return "success";
    }

    @RequestMapping(value="/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value="Accept") String header, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("RequestHeader");
        System.out.println(header);
        return "success";
    }

    @RequestMapping(value="/testCookieValue")
    public String testCookieValue(@CookieValue(value="JSESSIONID") String cookieValue){
        System.out.println("CookieValue");
        System.out.println(cookieValue);
        return "success";
    }

    @RequestMapping(value="/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("abc") User user){
        System.out.println("testModelAttribute");
        System.out.println(user);
        return "success";
    }

    /**
     * 修饰的方法有返回值
     * @param uname
     * @return
     */
    @ModelAttribute
    public User showUser(String uname){
        System.out.println("showUser执行了...");
        // 通过用户查询数据库（模拟）
        User user = new User();
        user.setUsername(uname);
        user.setAge(20);
        user.setDate(new Date());
        return user;
    }

    /**
     * 修饰的方法没有返回值
     * @param uname
     * @param map
     */
    @ModelAttribute
    public void showUser(String uname, Map<String,User> map){
        System.out.println("showUser执行了...");
        // 通过用户查询数据库（模拟）
        User user = new User();
        user.setUsername(uname);
        user.setAge(20);
        user.setDate(new Date());
        map.put("abc",user);
    }

    @RequestMapping(value="/testSessionAttributes")
    public String testSessionAttributes(Model model){
        System.out.println("testSessionAttributes");
        // 底层会存储到request域对象中
        model.addAttribute("msg","testSessionAttributes");
        return "success";
    }

    @RequestMapping(value="/getSessionAttributes")
    public String getSessionAttributes(ModelMap modelMap){
        System.out.println("getSessionAttributes");
        String msg = (String) modelMap.get("msg");
        System.out.println(msg);
        return "success";
    }

    @RequestMapping(value="/delSessionAttributes")
    public String delSessionAttributes(SessionStatus status){
        System.out.println("getSessionAttributes");
        status.setComplete();
        return "success";
    }


}
